/**
 * Settings模块 - 管理应用设置相关的状态
 */

const state = {
  // 主题设置
  theme: 'light', // light, dark, auto
  // 主色调
  primaryColor: '#1890ff',
  // 字体大小
  fontSize: 'medium', // small, medium, large
  // 动画效果
  animations: true,
  // 通知设置
  notifications: {
    desktop: true,
    email: true,
    sms: false,
    push: true
  },
  // 隐私设置
  privacy: {
    showProfile: true,
    showActivity: true,
    allowSearch: true
  },
  // 布局设置
  layout: {
    fixedHeader: true,
    fixedSidebar: true,
    multiTab: false
  },
  // 地区设置
  region: {
    timezone: 'Asia/Shanghai',
    dateFormat: 'YYYY-MM-DD',
    timeFormat: '24h'
  }
}

const mutations = {
  // 设置主题
  SET_THEME(state, theme) {
    state.theme = theme
  },
  
  // 设置主色调
  SET_PRIMARY_COLOR(state, color) {
    state.primaryColor = color
  },
  
  // 设置字体大小
  SET_FONT_SIZE(state, size) {
    state.fontSize = size
  },
  
  // 切换动画效果
  TOGGLE_ANIMATIONS(state) {
    state.animations = !state.animations
  },
  
  // 设置动画效果
  SET_ANIMATIONS(state, enabled) {
    state.animations = enabled
  },
  
  // 更新通知设置
  UPDATE_NOTIFICATIONS(state, notifications) {
    state.notifications = { ...state.notifications, ...notifications }
  },
  
  // 更新隐私设置
  UPDATE_PRIVACY(state, privacy) {
    state.privacy = { ...state.privacy, ...privacy }
  },
  
  // 更新布局设置
  UPDATE_LAYOUT(state, layout) {
    state.layout = { ...state.layout, ...layout }
  },
  
  // 更新地区设置
  UPDATE_REGION(state, region) {
    state.region = { ...state.region, ...region }
  },
  
  // 重置所有设置
  RESET_SETTINGS(state) {
    Object.assign(state, {
      theme: 'light',
      primaryColor: '#1890ff',
      fontSize: 'medium',
      animations: true,
      notifications: {
        desktop: true,
        email: true,
        sms: false,
        push: true
      },
      privacy: {
        showProfile: true,
        showActivity: true,
        allowSearch: true
      },
      layout: {
        fixedHeader: true,
        fixedSidebar: true,
        multiTab: false
      },
      region: {
        timezone: 'Asia/Shanghai',
        dateFormat: 'YYYY-MM-DD',
        timeFormat: '24h'
      }
    })
  }
}

const actions = {
  // 切换主题
  toggleTheme({ commit, state }) {
    const themes = ['light', 'dark']
    const currentIndex = themes.indexOf(state.theme)
    const nextTheme = themes[(currentIndex + 1) % themes.length]
    commit('SET_THEME', nextTheme)
    localStorage.setItem('theme', nextTheme)
  },
  
  // 设置主题
  setTheme({ commit }, theme) {
    commit('SET_THEME', theme)
    localStorage.setItem('theme', theme)
  },
  
  // 设置主色调
  setPrimaryColor({ commit }, color) {
    commit('SET_PRIMARY_COLOR', color)
    localStorage.setItem('primaryColor', color)
  },
  
  // 设置字体大小
  setFontSize({ commit }, size) {
    commit('SET_FONT_SIZE', size)
    localStorage.setItem('fontSize', size)
  },
  
  // 更新通知设置
  updateNotifications({ commit }, notifications) {
    commit('UPDATE_NOTIFICATIONS', notifications)
    localStorage.setItem('notifications', JSON.stringify(notifications))
  },
  
  // 更新隐私设置
  updatePrivacy({ commit }, privacy) {
    commit('UPDATE_PRIVACY', privacy)
    localStorage.setItem('privacy', JSON.stringify(privacy))
  },
  
  // 更新布局设置
  updateLayout({ commit }, layout) {
    commit('UPDATE_LAYOUT', layout)
    localStorage.setItem('layout', JSON.stringify(layout))
  },
  
  // 更新地区设置
  updateRegion({ commit }, region) {
    commit('UPDATE_REGION', region)
    localStorage.setItem('region', JSON.stringify(region))
  },
  
  // 保存所有设置
  saveSettings({ state }) {
    const settings = {
      theme: state.theme,
      primaryColor: state.primaryColor,
      fontSize: state.fontSize,
      animations: state.animations,
      notifications: state.notifications,
      privacy: state.privacy,
      layout: state.layout,
      region: state.region
    }
    localStorage.setItem('userSettings', JSON.stringify(settings))
  },
  
  // 加载设置
  loadSettings({ commit }) {
    const settings = localStorage.getItem('userSettings')
    if (settings) {
      const parsedSettings = JSON.parse(settings)
      commit('SET_THEME', parsedSettings.theme || 'light')
      commit('SET_PRIMARY_COLOR', parsedSettings.primaryColor || '#1890ff')
      commit('SET_FONT_SIZE', parsedSettings.fontSize || 'medium')
      commit('SET_ANIMATIONS', parsedSettings.animations !== false)
      commit('UPDATE_NOTIFICATIONS', parsedSettings.notifications || {})
      commit('UPDATE_PRIVACY', parsedSettings.privacy || {})
      commit('UPDATE_LAYOUT', parsedSettings.layout || {})
      commit('UPDATE_REGION', parsedSettings.region || {})
    }
  },
  
  // 重置设置
  resetSettings({ commit }) {
    commit('RESET_SETTINGS')
    localStorage.removeItem('userSettings')
  }
}

const getters = {
  // 是否为暗色主题
  isDarkTheme: (state) => state.theme === 'dark',
  
  // 是否为自动主题
  isAutoTheme: (state) => state.theme === 'auto',
  
  // 获取CSS变量格式的主色调
  primaryColorCSS: (state) => `var(--primary-color, ${state.primaryColor})`,
  
  // 获取字体大小的像素值
  fontSizeValue: (state) => {
    const sizes = {
      small: '12px',
      medium: '14px',
      large: '16px'
    }
    return sizes[state.fontSize] || sizes.medium
  },
  
  // 是否启用桌面通知
  desktopNotificationsEnabled: (state) => state.notifications.desktop,
  
  // 是否显示个人资料
  profileVisible: (state) => state.privacy.showProfile
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}